﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace XYS.Core.Util.Helper
{
    /// <summary>
    /// 正则 类
    /// </summary>
    public partial class RegexMatchesHelper
    {
        /// <summary>
        /// 匹配IP地址
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string TakeIPAddressRegex(string text)
        {
            var regex = new Regex(@"(\d+\.\d+\.\d+\.\d+)");
            var match = regex.Match(text);
            if (match.Success)
            {
                var ip = match.Groups[0].Value;
                return ip;
            }
            return "";
        }

        /// <summary>
        /// 是否为邮箱
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsEmail(string text)
        {
            string pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
            return Regex.IsMatch(text, pattern);
        }

        /// <summary>
        /// 是否为手机号
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsPhone(string text)
        {
            string pattern = "^1\\d{10}$";
            return Regex.IsMatch(text, pattern);
        }


        /// <summary>
        /// 是否是个性账户
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool IsAccount(string text)
        {
            //非数字开头
            //5-100位
            //只能包含数字 字母 下划线
            string pattern = "^[a-zA-Z]{1}([a-zA-Z0-9]|[._-]){4,100}$";
            return Regex.IsMatch(text, pattern);
        }


        /// <summary>
        /// 检查账号格式
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool CheckAccount(string text)
        {
            //必须以字母开头
            //3-100位
            //只能包含数字 字母 下划线 点 横杠
            string pattern = "^[a-zA-Z]{1}([a-zA-Z0-9]|[._-]){3,100}$";
            return Regex.IsMatch(text, pattern);
        }


    }
}
